package com.bite.springbook.Controller;

import com.bite.springbook.Model.Constants;
import com.bite.springbook.Model.Result;
import com.bite.springbook.Model.UserInfo;
import com.bite.springbook.Service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/login")
    public Result login(String username, String password, HttpSession session){

        UserInfo userInfo = userService.getUserByName(username);


        if(userInfo == null){
            return Result.fail("账号不存在");
        }
        if(!password.equals(userInfo.getPassword())){
            return Result.fail("账号或密码错误");
        }

        //设置session
        session.setAttribute(Constants.SESSION_USER_KEY, username);
        return Result.success("");
    }
}
